package com.ziyun.erp.modules.business.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.service.utils.sms.SmsResponse;
import com.service.utils.sms.SmsUtils;
import com.ziyun.erp.common.e.business.BusinessMessageTypeEnum;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.modules.business.dao.BusinessMessageDao;
import com.ziyun.erp.modules.business.entity.BusinessMessageEntity;
import com.ziyun.erp.modules.business.entity.BusinessMessagePeopleEntity;
import com.ziyun.erp.modules.business.service.BusinessMessagePeopleService;
import com.ziyun.erp.modules.business.service.BusinessMessageService;
import com.ziyun.erp.modules.member.service.UserBaseInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("businessMessageService")
public class BusinessMessageServiceImpl extends ServiceImpl<BusinessMessageDao, BusinessMessageEntity> implements BusinessMessageService {

    @Autowired
    private UserBaseInfoService userBaseInfoService;
    @Autowired
    private BusinessMessagePeopleService businessMessagePeopleService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<BusinessMessageEntity> page = this.selectPage(
                new Query<BusinessMessageEntity>(params).getPage(),
                new EntityWrapper<BusinessMessageEntity>().eq("`delete`", 0)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void insertOne(BusinessMessageEntity businessMessage, String phones) {
        List<String> phoneList = Lists.newArrayList();
        if (businessMessage.getType() == 1) {//导入手机号码
            //设置短信发送条数
            businessMessage.setSendCount(phones.split(",").length);
            // 发送短信
            // 接口是OK的,功能暂时未上线
      /*      JSONObject smsJson = new JSONObject();
            smsJson.put("username", "admin");
            smsJson.put("password", "123456");
            SmsResponse smsResponse = SmsUtils.sendLoginCode(phones, smsJson.toString());
            if (StringUtils.equals(smsResponse.getCode(), "OK")) {
                System.out.println("短信已成功发送至用户");
            } else {
                System.out.println("短信发送失败！");
            }*/
        } else if (businessMessage.getType() == 2) {//个性化营销
            switch (businessMessage.getPeopleName()) {
                case "3"://一般用户
                    BusinessMessagePeopleEntity peopleEntity = businessMessagePeopleService.selectById(3);
                    phoneList = userBaseInfoService.getMobileByDays(peopleEntity.getGapDay());
                    businessMessage.setSendCount(phoneList.size());
                    break;
                case "4"://供应商
//                    phoneList = userBaseInfoService.getMobileByDays(peopleEntity.getGapDay());
//                    businessMessage.setSendCount(phoneList.size());
                    break;
                case "5"://工厂
                    //...;
                    break;
                case "6"://企业用户
                    //...;
                    break;
            }

        } else if (businessMessage.getType() == 3) {//新客户

        } else {//老客户

        }

        businessMessage.setDelete(false);
        businessMessage.setStatus(0);
        //todo 发送短信,手机号码批量插入营销短信详情表
        this.baseMapper.insert(businessMessage);
    }

    @Override
    public R list(Map<String, Object> params) {
        PageUtils page = this.queryPage(params);
        //获取人群对象(排除新客户、老客户)
        List<BusinessMessagePeopleEntity> peopleEntityList = businessMessagePeopleService.selectList(new EntityWrapper<BusinessMessagePeopleEntity>().gt("id", 2));
        //todo 存放优惠券到userdata
        Map<String, List> mapEnum = new HashMap<>();
        mapEnum.put("peopleEntityList", peopleEntityList);
        mapEnum.put("messageTypeEnum", JsonUtils.enumToList(BusinessMessageTypeEnum.class));
        return R.ok().put("page", page).put("userData", mapEnum);
    }

}
